<template>
  <div>
    <HETTable1
      ref="UsbTable"
      :tableData="data.tableData"
      :tHead="data.tHead"
      :headerOperation="!treeStore.isShowStrategyDetail ? data.headerOperation : []"
      :checkBox="treeStore.isShowStrategyDetail ? false : true"
      :total="data.total"
      @chooseData="chooseData"
      @getTableData="getUsbList"
    >
      <template #inWlist>
        <el-table-column label="授信状态">
          <template #default="scope">
            <el-text type="primary" class="cursor-pointer" @click="changeEnable(scope.row)">{{
              scope.row.inWlist ? "取消授信" : "授信"
            }}</el-text>
          </template>
        </el-table-column>
      </template>
    </HETTable1>
  </div>
</template>

<script setup>
import { ref, reactive, markRaw, onMounted } from "vue";
import { Delete, Plus } from "@element-plus/icons-vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useTreeStore } from "~/utils/pinia/tree.js";
import {
  req_getPeriphrealList,
  req_delUsbList,
  req_addUsbNet,
  req_changeCredit,
} from "~/utils/axios/index.js";
import { eventBus } from "~/utils/common/eventBus.js";
const treeStore = useTreeStore();
const UsbTable = ref();
treeStore.treeLinkedTableRef = UsbTable;

eventBus.on("getStrategyDetail", (detail) => {
  data.tableData = detail.data;
  data.total = detail.total;
  treeStore.osType.value = detail.fromOs;
});
// 删除
const delUsb = () => {
  let usbIdList = data.selectData.map((item) => {
    return Number(item.id);
  });
  let delParams = {
    policyId: 0,
    usbList: usbIdList,
  };
  ElMessageBox.confirm("确定要删除选中的USB？", "删除USB", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    center: true,
    closeOnClickModal: false,
  }).then(() => {
    req_delUsbList(treeStore.treeDeviceId, delParams).then((res) => {
      res.data.succeed ? ElMessage.success("删除成功") : ElMessage.error("删除失败");
      UsbTable.value.getTableData();
    });
  });
};
// 添加
const addUsb = () => {
  let params = {
    policyId: 0,
    path: "",
    usb: 1,
    net: 2,
    isDeploy: 0,
  };
  req_addUsbNet(treeStore.treeDeviceId, params).then((res) => {
    res.data.succeed ? ElMessage.success("添加成功") : ElMessage.error("添加失败");
    UsbTable.value.getTableData();
  });
};
const data = reactive({
  tHead: [
    {
      label: "IP",
      prop: "ip",
      isCustom: false,
    },
    {
      label: "漏洞名称",
      prop: "name",
      isCustom: false,
      osType: "win",
    },
    {
      label: "描述",
      prop: "desc",
      isCustom: false,
    },
    {
      label: "CVE",
      prop: "cve",
      isCustom: false,
      osType: "linux",
    },
    {
      label: "漏洞等级",
      prop: "level",
      isCustom: true,
      slotName: "inWlist",
      osType: "linux",
    },
  ],
  tableData: [],
  headerOperation: [
    {
      name: "添加",
      icon: markRaw(Plus),
      handleClick: addUsb,
    },
    {
      name: "删除",
      icon: markRaw(Delete),
      handleClick: delUsb,
    },
  ],
  selectData: [],
  total: null,
});

// 多选
const chooseData = (selectData) => {
  data.selectData = selectData;
};

// linux更改授信状态
const changeEnable = (row) => {
  let params = {
    idList: [row.id],
    type: "usb",
    enable: row.inWlist == 1 ? false : true,
  };
  req_changeCredit(treeStore.treeDeviceId, params).then((res) => {
    res.data.succeed ? ElMessage.success("操作成功") : ElMessage.error("操作失败");
    UsbTable.value.getTableData();
  });
};

// 获取usb表格数据
const getUsbList = async (initTableParams, $filter) => {
  if (!treeStore.treeDeviceId) return;
  UsbTable.value.loading();
  initTableParams = { ...initTableParams, policyId: 0, type: 2 };
  try {
    const { data: res } = await req_getPeriphrealList(
      treeStore.treeDeviceId,
      initTableParams
    );
    UsbTable.value.finish();
    data.tableData = res.data || [];
    data.total = res.total || 0;
    data.selectData = [];
  } catch (err) {
    UsbTable.value.finish();
    data.tableData = [];
    data.total = 0;
    data.selectData = [];
  }
};
</script>

<style lang="scss" scoped></style>
